/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.bs.user;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.MySQLConn;

/**
 *
 * @author RdX
 */
public class Authentication {

   private static String generatePassword() //6 digit random password
   {
      Random randomGenerator = new Random();
      int max = 122 - 48;
      StringBuilder b = new StringBuilder();
      int randomInt;
      for (int i = 0; i < 6; i++) {
         randomInt = randomGenerator.nextInt(max + 1) + 48;
         b.append((char) randomInt);
      }
      return b.toString();
   }

   public static int validateLogin(String user, String pass) {
      String query = "Select * from auth where userid='" + user + "' and pass='" + pass + "'";
      String fTime="Select firsttime from auth where userid='" + user + "' and pass='" + pass + "' and firsttime='y'";
      System.out.println(query);
      System.out.println(fTime);
      Connection auth = MySQLConn.getConnection();
      ResultSet rs = null;
      ResultSet ft=null;
      int flag = 0;
      try {
         rs = auth.createStatement().executeQuery(query);
         ft=auth.createStatement().executeQuery(fTime);
         //System.out.println(ft.next());
         if(rs.next())
         {
            switch(user.charAt(0))
            {
               case 'S':
                  flag=1;
                  break;
               case 'E':
                  flag=2;
                  break;
               case 'A':
                  flag=3;
                  break;
         }
              if(!ft.next() && flag!=0)
             flag+=3;
         auth.close();
         }
      } catch (SQLException ex) {
         System.out.println("Query not executed");
      }
      
      
      return flag;
   }

   public static int addLogin(String user, String pass) {
      String query = "insert into auth values('" + user + "','" + pass + "','y')";
      System.out.println(query);
      Connection auth = MySQLConn.getConnection();
      ResultSet rs = null;
      int rowsAffected = 0;
      try {
         rowsAffected = auth.createStatement().executeUpdate(query);
         auth.close();
      } catch (SQLException ex) {
         System.out.println("Query not executed");
      }

      return rowsAffected;
   }

   public int addTestCases() {
      Connection addCase = MySQLConn.getConnection();
      String[] uid = {"A000001", "E000001", "E000002", "S000001", "S000002", "S000003"};
      int ctr = 0;
      for (int i = 0; i < 6; i++) {
         String randNum = Authentication.generatePassword();
         System.out.println(uid[i] + "     " + randNum);
         ctr += Authentication.addLogin(uid[i], randNum);

      }
      System.out.println(ctr + " rows inserted");
      return ctr;
   }

   public static void main(String args[]) {
         System.out.println(Authentication.validateLogin("E000004", "TQu;O>"));
   }
}
